<script setup>
import { ref,reactive,unref } from 'vue'
import useMemberStore from '../../store/memberStore'
import { ElMessageBox } from 'element-plus'

const memberStore = useMemberStore()

const editId = ref('')
const memberForm = reactive({
  uid: "",
  nickname: "",
  phone: "",
  password: "",
  status: 1
})
const rules = reactive({
  nickname: {
    required: true,
    message: "请输入昵称",
    trigger: "blur",
  },
  phone: {
    required: true,
    message: "请输入昵称",
    trigger: "blur",
  },
  password: {
    required: true,
    message: "请输入昵称",
    trigger: "blur",
  },
  status: { required: true, message: "请选择状态", trigger: "blur" }
})
const formRef = ref(null)

// 导出父组件需要用的属性与方法
defineExpose({
  editId,memberForm
})


// 重置表单
const resetForm = () => {
  editId.value = ''
  memberForm.uid = ''
  memberForm.nickname = ''
  memberForm.phone = ''
  memberForm.password = ''
  memberForm.status = 1
  // 关闭对话框
  memberStore.closeDialog()
}
// 提交表单
const submitForm = () => {
  let form = unref(formRef)
  form.validate((valid) => {
    if (valid) {
      // console.log('提交商品', memberForm)
      if(editId.value) {
        // 编辑
        // console.log(editId,'编辑-----')
        delete memberForm["id"]
        delete memberForm["addtime"]
        delete memberForm["randstr"]
        memberStore.editMemberAction(memberForm)
      } else {
        // console.log('新增----')
      }
      // 重置表单
      resetForm()
    } else {
      // console.log('error submit!')
      return false
    }
  })
}
const handleClose = (done) => {
  ElMessageBox.confirm('你确定要关闭吗？').then(() => {
    done()
    resetForm()
  }).catch(() => {
    // catch error
  })
}
</script>

<template>
  <!-- 对话框 -->
  <el-dialog
    :title="editId ? '编辑会员' : '新增会员'"
    v-model="memberStore.dialogVisible"
    width="50%"
    :before-close="handleClose"
  >
    <el-form
      :model="memberForm"
      :rules="rules"
      ref="formRef"
      label-width="100px"
    >
      <el-form-item label="会员编号" prop="title">
        <el-input v-model="memberForm.uid" placeholder="请输入图片名称" />
      </el-form-item>
      <el-form-item label="会员昵称" prop="title">
        <el-input v-model="memberForm.nickname" placeholder="请输入会员昵称" />
      </el-form-item>
      <el-form-item label="手机号码" prop="title">
        <el-input v-model="memberForm.phone" placeholder="请输入手机号码" />
      </el-form-item>
      <el-form-item label="登录密码" prop="title">
        <el-input v-model="memberForm.password" placeholder="请输入登录密码" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="memberForm.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="submitForm">{{ editId? '编辑会员':'新增会员' }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>

</style>
